using Pegasus.Diagnostics;
using System;
using System.Collections;

namespace Pegasus.Configuration
{
	/// <summary>
	/// Used to parse the command line parameters and provide simple access methods to the elements
	/// </summary>
	public class CommandLine
	{
		// Local Instance Values
		private Hashtable m_optionsTable = new Hashtable();
		private ArrayList m_valueList = new ArrayList();

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="args">The command line arguments</param>
		public CommandLine( string[] args )
		{
			// Check params
			ParamCode.AssertNotNull( args, "args" );

			int count = args.Length;
			for( int x = 0; x < count; x++ )
			{
				string option = args[ x ];

				if( option.Length > 2 && ( option.StartsWith( "/" ) || option.StartsWith( "-" ) ) )
				{
					option = option.Substring( 1 ).ToLower();

					if( x + 1 < count )
					{
						string val = args[ x + 1 ];
						if( val.Length > 2 && ( val.StartsWith( "/" ) || val.StartsWith( "-" ) ) )
						{
							m_optionsTable[ option ] = string.Empty;
						}
						else
						{
							m_optionsTable[ option ] = val;
							x++;
						}
					}
					else
					{
						m_optionsTable[ option ] = string.Empty;
					}
				}
				else
				{
					m_valueList.Add( option );
				}
			}
		}

		/// <summary>
		/// Will check to see if an option exist on the command line
		/// </summary>
		/// <param name="option">The name of the option without a / or -.</param>
		/// <returns></returns>
		public bool Exist( string option )
		{
			return m_optionsTable.ContainsKey( option.ToLower() );
		}
	}
}
